package com.itheima.service.store.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.store.CompanyDao;
import com.itheima.domain.store.Company;
import com.itheima.factory.MapperFactory;
import com.itheima.service.store.CompanyService;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.UUID;

/**
 * @program: mm
 * @description:
 * @author: zhanghz001
 * @create: 2020-10-12 17:49
 **/
public class CompanyServiceImpl implements CompanyService {
    @Override
    public void save(Company company) {
        SqlSession sqlSession = null;
        try {
            //    sqlSession

            sqlSession = MapperFactory.getSqlSession();
            //companyDao
            CompanyDao companyDao = MapperFactory.getMapper(sqlSession, CompanyDao.class);
            //  准备插入数据,id准备
            company.setId(UUID.randomUUID().toString());
            //保存了数据
            companyDao.save(company);
            // 提交

            //释放sqlsession
            TransactionUtil.commit(sqlSession);

            //

        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
        } finally {
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public void delete(Company company) {
        SqlSession sqlSession = null;
        try {
            //    获取sqlSession

            sqlSession = MapperFactory.getSqlSession();
            //获取dao
            CompanyDao companyDao = MapperFactory.getMapper(sqlSession, CompanyDao.class);

            //调用dao层操作
            companyDao.delete(company);
            // 提交事务

            TransactionUtil.commit(sqlSession);

            //

        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        } finally {
            //释放sqlsession
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public void update(Company company) {

        SqlSession sqlSession = null;
        try {
            //    获取sqlSession

            sqlSession = MapperFactory.getSqlSession();
            //获取dao
            CompanyDao companyDao = MapperFactory.getMapper(sqlSession, CompanyDao.class);

            //调用dao层操作
            companyDao.update(company);
            // 提交事务

            TransactionUtil.commit(sqlSession);

            //

        } catch (Exception e) {
            TransactionUtil.rollback(sqlSession);
            throw new RuntimeException(e);
            //记录日志
        } finally {
            //释放sqlsession
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public Company findById(String id) {

        SqlSession sqlSession = null;
        try {
            //    获取sqlSession

            sqlSession = MapperFactory.getSqlSession();
            //获取dao
            CompanyDao companyDao = MapperFactory.getMapper(sqlSession, CompanyDao.class);

            //调用dao层操作
            Company byId = companyDao.findById(id);
            return byId;
            // 提交事务

            //

        } catch (Exception e) {
            throw new RuntimeException(e);
            //记录日志
        } finally {
            //释放sqlsession
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public List<Company> findAll() {

        SqlSession sqlSession = null;
        try {
            //    获取sqlSession

            sqlSession = MapperFactory.getSqlSession();
            //获取dao
            CompanyDao companyDao = MapperFactory.getMapper(sqlSession, CompanyDao.class);

            //调用dao层操作
            return companyDao.findAll();

            // 提交事务

            //

        } catch (Exception e) {
            throw new RuntimeException(e);
            //记录日志
        } finally {
            //释放sqlsession
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public PageInfo findAll(int page, int size) {

        SqlSession sqlSession = null;
        try {
            //    获取sqlSession

            sqlSession = MapperFactory.getSqlSession();
            //获取dao
            CompanyDao companyDao = MapperFactory.getMapper(sqlSession, CompanyDao.class);

            //调用dao层操作
            PageHelper.startPage(page, size);
            List<Company> all = companyDao.findAll();
            PageInfo info = new PageInfo<>(all);
            return info;
            // 提交事务

            //

        } catch (Exception e) {
            throw new RuntimeException(e);
            //记录日志
        } finally {
            //释放sqlsession
            try {
                TransactionUtil.close(sqlSession);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
